package de.uni.bremen.estudent.lbs.tasks;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;

import com.google.android.maps.GeoPoint;

import de.uni.bremen.estudent.lbs.R;
import de.uni.bremen.estudent.lbs.models.LbsOverlayItem;
import de.uni.bremen.estudent.lbs.models.MyItemizedOverlay;
import de.uni.bremen.estudent.lbs.provider.EstudentLBSContract.Categories;
import de.uni.bremen.estudent.lbs.provider.EstudentLBSContract.Pois;
import de.uni.bremen.estudent.lbs.ui.phone.HomeActivity;
import de.uni.bremen.estudent.lbs.util.Constants;

public class OverlayAsyncTask extends AsyncTask<Void, Void, MyItemizedOverlay> {

    /**
     * These are the poi rows that we will retrieve.
     */
    static final String[] POIS_SUMMARY_PROJECTION = new String[] { Pois.POI_ID,
            Pois.POI_NAME, Pois.POI_LAT, Pois.POI_LON, Pois.POI_CATEGORY,
            Pois.POI_DESCRIPTION, Pois.POI_OPENING_HOURS };
    /**
     * Drawables for the overlays.
     */
    private Drawable buildingDrawable, stationDrawable, libDrawable,
            foodDrawable, mKioskDrawable, mServiceDrawable, mSwimmingDrawable,
            mSportDrawable, buildingDrawableTransparent,
            stationDrawableTransparent, libDrawableTransparent,
            foodDrawableTransparent, mKioskDrawableTransparent,
            mServiceDrawableTransparent, mSwimmingDrawableTransparent,
            mSportDrawableTransparent;
    private final HomeActivity mActivity;

    private final Context mContext;

    private final int alphaValue = 0;

    private MyItemizedOverlay mItemizedOverlay;

    public OverlayAsyncTask(HomeActivity activity) {
        this.mActivity = activity;
        this.mContext = this.mActivity;
    }

    @Override
    protected MyItemizedOverlay doInBackground(Void... params) {

        // Load drawables
        final Resources res = this.mContext.getResources(); // avoid method
        // calls!!!

        this.buildingDrawable = res.getDrawable(R.drawable.gebaeude);
        this.buildingDrawable.setBounds(0, 0,
                this.buildingDrawable.getIntrinsicWidth(),
                this.buildingDrawable.getIntrinsicHeight());

        this.buildingDrawableTransparent = res
                .getDrawable(R.drawable.gebaeude_off);
        this.buildingDrawableTransparent.setBounds(0, 0,
                this.buildingDrawable.getIntrinsicWidth(),
                this.buildingDrawable.getIntrinsicHeight());
        // this.buildingDrawableTransparent.setAlpha(this.alphaValue);

        this.libDrawable = res.getDrawable(R.drawable.bilbiothek);
        this.libDrawable.setBounds(0, 0, this.libDrawable.getIntrinsicWidth(),
                this.libDrawable.getIntrinsicHeight());

        this.libDrawableTransparent = res.getDrawable(R.drawable.bilbiothek);
        this.libDrawableTransparent.setBounds(0, 0,
                this.libDrawable.getIntrinsicWidth(),
                this.libDrawable.getIntrinsicHeight());
        // this.libDrawableTransparent.setAlpha(this.alphaValue);


        this.foodDrawable = res.getDrawable(R.drawable.essen);
        this.foodDrawable.setBounds(0, 0,
                this.foodDrawable.getIntrinsicWidth(),
                this.foodDrawable.getIntrinsicHeight());

        this.foodDrawableTransparent = res.getDrawable(R.drawable.essen_off);
        this.foodDrawableTransparent.setBounds(0, 0,
                this.foodDrawable.getIntrinsicWidth(),
                this.foodDrawable.getIntrinsicHeight());
        // this.foodDrawableTransparent.setAlpha(this.alphaValue);

        this.mKioskDrawable = res.getDrawable(R.drawable.kiosk);
        this.mKioskDrawable.setBounds(0, 0,
                this.mKioskDrawable.getIntrinsicWidth(),
                this.mKioskDrawable.getIntrinsicHeight());

        this.mKioskDrawableTransparent = res.getDrawable(R.drawable.kiosk_off);
        this.mKioskDrawableTransparent.setBounds(0, 0,
                this.mKioskDrawable.getIntrinsicWidth(),
                this.mKioskDrawable.getIntrinsicHeight());
        // this.mKioskDrawableTransparent.setAlpha(this.alphaValue);

        this.mServiceDrawable = res.getDrawable(R.drawable.dienstleistung);
        this.mServiceDrawable.setBounds(0, 0,
                this.mServiceDrawable.getIntrinsicWidth(),
                this.mServiceDrawable.getIntrinsicHeight());

        this.mServiceDrawableTransparent = res
                .getDrawable(R.drawable.dienstleistung_off);
        this.mServiceDrawableTransparent.setBounds(0, 0,
                this.mServiceDrawable.getIntrinsicWidth(),
                this.mServiceDrawable.getIntrinsicHeight());
        // this.mServiceDrawableTransparent.setAlpha(this.alphaValue);

        this.mSwimmingDrawable = res.getDrawable(R.drawable.swimming_on);
        this.mSwimmingDrawable.setBounds(0, 0,
                this.mSwimmingDrawable.getIntrinsicWidth(),
                this.mSwimmingDrawable.getIntrinsicHeight());

        this.mSwimmingDrawableTransparent = res
                .getDrawable(R.drawable.swimming_off);
        this.mSwimmingDrawableTransparent.setBounds(0, 0,
                this.mSwimmingDrawable.getIntrinsicWidth(),
                this.mSwimmingDrawable.getIntrinsicHeight());
        // this.mSwimmingDrawableTransparent.setAlpha(this.alphaValue);

        this.mSportDrawable = res.getDrawable(R.drawable.sport_on);
        this.mSportDrawable.setBounds(0, 0,
                this.mSportDrawable.getIntrinsicWidth(),
                this.mSportDrawable.getIntrinsicHeight());

        this.mSportDrawableTransparent = res.getDrawable(R.drawable.sport_off);
        this.mSportDrawableTransparent.setBounds(0, 0,
                this.mSportDrawable.getIntrinsicWidth(),
                this.mSportDrawable.getIntrinsicHeight());
        // this.mSportDrawableTransparent.setAlpha(this.alphaValue);

        this.mItemizedOverlay = new MyItemizedOverlay(this.buildingDrawable,
                this.mContext);

        // Loading the pois...
        Uri pois = Pois.CONTENT_URI;
        Cursor cursor = this.mActivity.managedQuery(pois,
                POIS_SUMMARY_PROJECTION, "", null, null);
        this.itemsgetColumnData(cursor);

        return this.mItemizedOverlay;
    }

    @Override
    protected void onPostExecute(MyItemizedOverlay itemizedOverlay) {
        this.mActivity.updateOverlayItems(itemizedOverlay);
    }

    private void itemsgetColumnData(Cursor managedCursor) {
        if (this.mItemizedOverlay != null) {
            if (managedCursor.moveToFirst()) {

                SharedPreferences settings = this.mContext
                        .getSharedPreferences(Constants.PREFS_NAME, 0);
                boolean showBuilding = settings.getBoolean(Constants.GEBAEUDE,
                        true);
                boolean showFoodService = settings.getBoolean(
                        Constants.GASTRONOMIE, true);
                boolean showLibary = settings.getBoolean(Constants.BIBLIOTHEK,
                        true);
                boolean showService = settings.getBoolean(
                        Constants.DIENSTLEISTUNGEN, true);

                boolean showKiosk = settings.getBoolean(Constants.KIOSK, true);

                boolean showSwimming = settings.getBoolean(Constants.SCHWIMMEN,
                        true);

                boolean showSport = settings.getBoolean(Constants.SPORT, true);

                String name;
                String categoryName;
                String desc;
                String opening;
                String contact;
                double lat;
                double lon;
                int catNameColumn = managedCursor
                        .getColumnIndex(Categories.CATEGORY_NAME);
                int nameColumn = managedCursor.getColumnIndex(Pois.POI_NAME);
                int latColumn = managedCursor.getColumnIndex(Pois.POI_LAT);
                int lonColumn = managedCursor.getColumnIndex(Pois.POI_LON);
                int contactColumn = managedCursor
                        .getColumnIndex(Pois.POI_CONTACT);
                int descriptionColumn = managedCursor
                        .getColumnIndex(Pois.POI_DESCRIPTION);
                int openingHoursColumn = managedCursor
                        .getColumnIndex(Pois.POI_OPENING_HOURS);

                do {
                    categoryName = managedCursor.getString(catNameColumn);
                    lat = managedCursor.getDouble(latColumn);
                    lon = managedCursor.getDouble(lonColumn);
                    name = managedCursor.getString(nameColumn);
                    desc = managedCursor.getString(descriptionColumn);
                    contact = managedCursor.getString(contactColumn);
                    opening = managedCursor.getString(openingHoursColumn);

                    GeoPoint point = new GeoPoint(
                            (int) (lat * Constants.GEO_POINT_UNIT),
                            (int) (lon * Constants.GEO_POINT_UNIT));
                    LbsOverlayItem overlayitem = new LbsOverlayItem(point,
                            name, desc, categoryName, contact, opening);
                    if (Constants.GEBAEUDE.equals(categoryName)) {
                        if (!showBuilding) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.buildingDrawable);
                        } else {
                            overlayitem
                                    .setMarker(this.buildingDrawableTransparent);
                        }
                    } else if (Constants.BIBLIOTHEK.equals(categoryName)) {
                        if (!showLibary) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.libDrawable);
                        } else {
                            overlayitem.setMarker(this.libDrawableTransparent);
                        }
                    } else if (Constants.GASTRONOMIE.equals(categoryName)) {
                        if (!showFoodService) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.foodDrawable);
                        } else {
                            overlayitem.setMarker(this.foodDrawableTransparent);
                        }
                    } else if (Constants.KIOSK.equals(categoryName)) {
                        if (!showKiosk) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.mKioskDrawable);
                        } else {
                            overlayitem
                                    .setMarker(this.mKioskDrawableTransparent);
                        }
                    } else if (Constants.DIENSTLEISTUNGEN.equals(categoryName)) {
                        if (!showService) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.mServiceDrawable);
                        } else {
                            overlayitem
                                    .setMarker(this.mServiceDrawableTransparent);
                        }
                    } else if (Constants.SCHWIMMEN.equals(categoryName)) {
                        if (!showSwimming) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.mSwimmingDrawable);
                        } else {
                            overlayitem
                                    .setMarker(this.mSwimmingDrawableTransparent);
                        }
                    } else if (Constants.SPORT.equals(categoryName)) {
                        if (!showSport) {
                            continue;
                        }
                        if (overlayitem.isOpen()) {
                            overlayitem.setMarker(this.mSportDrawable);
                        } else {
                            overlayitem
                                    .setMarker(this.mSportDrawableTransparent);
                        }
                    }
                    this.mItemizedOverlay.addOverlay(overlayitem);

                } while (managedCursor.moveToNext());
            }
        }
    }

}
